package com.dss.auto_deploy.service.impl;

import com.dss.auto_deploy.config.WebsocketEndPoint;
import com.dss.auto_deploy.controller.DockerMergeLogsWsServer;
import com.dss.auto_deploy.controller.DockerSingleLogsWsServer;
import com.dss.auto_deploy.controller.MergeLogsWsServer;
import com.dss.auto_deploy.controller.SingleLogsWsServer;
import com.dss.auto_deploy.mapper.AdmDockerDeployMapper;
import com.dss.auto_deploy.mapper.IDeployMapper;
import com.dss.auto_deploy.service.IAdmDockerDeployService;
import com.dss.auto_deploy.service.IAdmDockerService;
import com.dss.auto_deploy.service.IDeployService;
import com.dss.auto_deploy.util.DockerFileProgressMonitor;
import com.dss.auto_deploy.util.FileProgressMonitor;
import com.dss.auto_deploy.util.IpUtil;
import com.dss.auto_deploy.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @Description:
 * @author: dongshoushan
 */
@Order(value = 1)
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class InitRun implements CommandLineRunner {
    private final IDeployService deployService;
    private final IAdmDockerDeployService admDockerDeployService;
    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;
    private final ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
    private final WebsocketEndPoint websocketEndPoint;
    private final IDeployMapper deployMapper;
    private final AdmDockerDeployMapper admDockerDeployMapper;
    private final IAdmDockerService admDockerService;
    @Value("${server.port}")
    private  String port;
    @Override
    public void run(String... args) throws Exception {
        System.out.println("============项目启动，清空上一次的部署日志，重置所有部署状态为初始化============");

        MergeLogsWsServer.setThreadPoolTaskExecutor(threadPoolTaskExecutor);
        DockerMergeLogsWsServer.setThreadPoolTaskExecutor(threadPoolTaskExecutor);
        SingleLogsWsServer.setThreadPoolTaskExecutor(threadPoolTaskExecutor);
        SingleLogsWsServer.setDeployMapper(deployMapper);
        DockerSingleLogsWsServer.setThreadPoolTaskExecutor(threadPoolTaskExecutor);
        DockerSingleLogsWsServer.setAdmDockerDeployMapper(admDockerDeployMapper);

        FileProgressMonitor.setWebsocketEndPoint(websocketEndPoint);
        FileProgressMonitor.setDeployMapper(deployMapper);
        FileProgressMonitor.setScheduledThreadPoolExecutor(scheduledThreadPoolExecutor);

        DockerFileProgressMonitor.setScheduledThreadPoolExecutor(scheduledThreadPoolExecutor);
        DockerFileProgressMonitor.setAdmDockerDeployMapper(admDockerDeployMapper);
        DockerFileProgressMonitor.setWebsocketEndPoint(websocketEndPoint);

        deployService.init();
        admDockerDeployService.init();
        admDockerService.init();

        String localIp = IpUtil.getLocalIp();
        System.out.println("自动部署工具启动成功");
        System.out.println(StringUtils.format("首页 http://{}:{}/page/index.html",localIp,port));
        System.out.println(StringUtils.format("swagger http://{}:{}/doc.html",localIp,port));
        //        Runtime.getRuntime().exec(StringUtils.format("cmd   /c   start  http://{}:8088/page/index.html",localIp));
    }
}
